package com.company.cloud.common.utils;

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.StringUtils;

import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.constant.BaseConstant.LangEnum;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.constant.CommonConstant;
import com.company.cloud.common.exception.BusinessException;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;

public class AuthContextUtils {

	/**
	 * 用户登录信息转字符串
	 * */
	public static String getUserStr(LoginUserInfo user) {
		LoginSimpleUserInfo simpleUser = new LoginSimpleUserInfo();
		simpleUser.setId(user.getId());
		simpleUser.setCompanyNo(user.getCompanyNo());
		simpleUser.setUserTypeOne(user.getUserTypeOne());
		simpleUser.setUserTypeTwo(user.getUserTypeTwo());
		simpleUser.setUserName(user.getUserName());
		simpleUser.setIp(user.getIp());
		simpleUser.setToken(user.getToken());
		simpleUser.setThirdId(user.getThirdId());
		simpleUser.setThirdUserId(user.getThirdUserId());
		simpleUser.setThirdOpenId(user.getThirdOpenId());
		simpleUser.setKey(IdUtil.fastSimpleUUID());
		return simpleUser.toString();
	}
	
	/**
	 * 用户登录信息转字符串（base64编码）
	 * */
	public  static String getUserBase64(LoginUserInfo user) {
		String val=AuthContextUtils.getUserStr(user);
		return Base64.encode(val, "UTF-8");
	}

	/**
	 * 字符串(base64编码)转用户信息
	 * */
	public static LoginSimpleUserInfo getUserByBase64(String val) {
		if(StringUtils.isEmpty(val)) {
			return null;
		}
		String userStr = Base64.decodeStr(val, "UTF-8");
		return getUserByStr(userStr);
	}

	/**
	 * 字符串转用户信息
	 * */
	public static LoginSimpleUserInfo getUserByStr(String val) {
		if(StringUtils.isEmpty(val)) {
			return null;
		}
		return new LoginSimpleUserInfo(val);
	}
	
	
	/**
	 * 简单用户信息转登录用户信息
	 * */
	public static LoginUserInfo getUserBySimple(LoginSimpleUserInfo loginSimpleUser) {
		LoginUserInfo data=new LoginUserInfo();
		BeanUtil.copyProperties(loginSimpleUser, data);
		return data;
	}
	
	public static String getIp() {
		return HttpContextUtils.getClientIP();
	}

	
	/**
	 * header：获取用户信息,校验(无报错)
	 * */
	public static LoginSimpleUserInfo getUser() throws BusinessException {
		HttpServletRequest request = HttpContextUtils.getRequest();
		String val = request.getHeader(CommonConstant.USER_HEADER);
		if(StringUtils.isEmpty(val)) {
			throw new BusinessException(BaseReturnEnum.NOT_LOGIN);
		}
		return AuthContextUtils.getUserByBase64(val);
	}
	
	/**
	 * header:获取用户信息,不校验
	 * */
	public static LoginSimpleUserInfo getUserNoValidate() {
		HttpServletRequest request = HttpContextUtils.getRequest();
		String val = request.getHeader(CommonConstant.USER_HEADER);
		return AuthContextUtils.getUserByBase64(val);
	}
	
	/**
	 * header:获取公司编号:不校验
	 * */
	public static String getCompanyNoNoValidate() {
		HttpServletRequest request = HttpContextUtils.getRequest();
		return request.getHeader(CommonConstant.TENANT_HEADER);
	}
	
	/**
	 * header:获取推荐码（分享码）
	 * */
	public static String getShareCode() {
		HttpServletRequest request = HttpContextUtils.getRequest();
		return request.getHeader(CommonConstant.SHARE_HEADER);
	}
	
	/**
	 * header:获取匿名用户标识
	 * */
	public static String getAnonyUserToken() {
		HttpServletRequest request = HttpContextUtils.getRequest();
		return request.getHeader(CommonConstant.ANONY_TOKEN_HEADER);
	}


	/**
	 * header:获取公司编号(空值校验)
	 * */
	public static String getCompanyNo() {
		HttpServletRequest request = HttpContextUtils.getRequest();
		String saas = request.getHeader(CommonConstant.TENANT_HEADER);
		if (StringUtils.isEmpty(saas)) {
			throw new BusinessException(BaseReturnEnum.CompanyNo_NULL);
		}
		return saas;
	}
	
	

	/**
	 * header:获取语言编号
	 * */
	public static String getLang() throws BusinessException{
		return LangEnum.ZH.getCode();
	}
}
